<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');

class To_do extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		// check if user logged in
		if(!$this->ion_auth->logged_in()){
			redirect('auth/login');	
			}
		$this->load->model('user/todo_model');
		$this->load->model('user/user_model');
		$this->load->library('form_validation');
		
	}

public function view($userid=NULL,$month=NULL,$year=NULL){	
	$this->meta['page_title'] = 'To Do List';
	$this->data['month_id'] = $month;
	$this->data['userid'] = $userid;
	$this->data['year_id'] = $year;
	$this->data['todos'] = $this->todo_model->view($userid,$month,$year);
	
	$this->data['fav_todos'] = $this->user_model->fav_todo($userid);
	
	$this->load->view('common/header',$this->meta);
	$this->load->view('user/to-do',$this->data);
	$this->load->view('common/footer');
	
}
	public function save($user_id=null){
	$input = $this->input->post();	
	//$is_fav = $input['is_fav'];
	$date  = explode('-',$input['date']);
	$date_id = $date[2];
	$month_id = $date[1];	
	$year_id = str_replace('20','',$date[0]);
	//$user_id = isse	
	if(!empty($input['priority_is']))
	{
		$priority_is = $input['priority_is'];
	}else{
		$priority_is = "";
	}
	if(empty($input['user_id'])){
	$user_id = $user_id;	
	}
	else{
		$user_id = $input['user_id'];
		//$user_id = $user_id;
	}
	if(!empty($input['is_fav']))
	{
		$is_fav = $input['is_fav'];
	}
	else
	{
		$is_fav = 0;
	}
	
	$insert = array(
	'date_id' => $date_id,
	'month_id' => $month_id,
	'year_id' => $year_id,
	'user_id' => $user_id,
	'des' => $input['desc'],
	'priority' => $priority_is,
	'subtask_id' => 0,
	'is_fav' => $is_fav,		
	);
	
	$save = $this->todo_model->save($insert);
	$this->session->set_flashdata('msg','Added Succesfully');
	if(empty($input['user_id'])){		
		redirect('user/to_do/view/'.$user_id.'/'.$month_id.'/'.$year_id,'refresh');	
	}else{ redirect('user/to_do/view_all/'.$date_id.'/'.$month_id.'/'.$year_id);}		
}

public function save_from_subtask(){	
	$input = $this->input->post();	
	$date_id = $input['date'];
	$month_id = $input['month'];	
	$year_id = $input['year'];
	$user_id = $input['user'];
	$task_id = $input['tasks_id'];
	$url = site_url("user/tasks/task_detail/$task_id");
	$desc = '<a href="'.$url.'" target="_blank">'.$input['desc'].'</a>';	
	$insert = array(
	'date_id' => $date_id,
	'month_id' => $month_id,
	'year_id' => $year_id,
	'user_id' => $user_id,
	'des' => $desc,
	'subtask_id' => $input['subtask_id'],	
	);		
	$save = $this->todo_model->save($insert);
	$this->session->set_flashdata('msg','Added Succesfully');
	redirect('user/tasks/task_detail/'.$task_id);	
}

	public function delete(){
		$id = $_REQUEST['id'];
		$this->todo_model->delete($id);
	}
	public function add_to_subtask(){
		$des = $_REQUEST['des'];		
		$this->todo_model->add_workshhet($des);		
	}
	
	public function add_to_worksheet(){
		$id = $_REQUEST['id'];
	}
	public function worksheet($id){
		$this->data['todo'] = $this->todo_model->get_details($id);
		$this->data['users'] = 'sadas';
		$this->load->view('user/add_todo_worksheet',$this->data);
	}
	
	public function get_todo($id){
		$data['todo'] = $this->todo_model->get_details($id);
		$this->load->view('user/edit_todo',$data);
	}
	
	/*public function update_todo(){
		$input = $this->input->post();
		$date_id = $input['date'];
		$month_id = $input['month'];	
		$year_id = $input['year'];
		$desc = $input['desc'];
		$user_id = $input['user_id'];
		
		$update = array(
		'date_id' => $date_id,
		'month_id' => $month_id,
		'year_id'=> $year_id,
		'user_id'=> $user_id,
		'des'=> $desc,
		);		
		$this->todo_model->update($update,$input['id']);
		redirect('user/to_do/view/'.$user_id.'/'.$month_id.'/'.$year_id);
	}*/
	
	public function update_todo(){
		$input = $this->input->post();
		$date_id = $input['date'];
		$month_id = $input['month'];	
		$year_id = $input['year'];
		$desc = $input['desc'];
		$user_id = $input['user_id'];
		$priority_is = $input['priority_is'];
		
		$update = array(
		'date_id' => $date_id,
		'month_id' => $month_id,
		'year_id'=> $year_id,
		'user_id'=> $user_id,
		'des'=> $desc,
		'priority'=>$priority_is,
		);		
		$this->todo_model->update($update,$input['id']);
		redirect('user/to_do/view/'.$user_id.'/'.$month_id.'/'.$year_id);
	}
	
	public function complete($id,$dateid,$monthid,$yearid){	
		$todo = $this->todo_model->get_details($id);
		$this->todo_model->complete($id,$dateid,$monthid,$yearid);
		
		redirect('user/to_do/view/'.$todo['user_id'].'/'.$monthid.'/'.$yearid);
	}
	
	public function incomplete($id){
		$todo = $this->todo_model->get_details($id);
		$this->todo_model->incomplete($id);
		redirect('user/to_do/view/'.$todo['user_id'].'/'.$todo['month_id'].'/'.$todo['year_id']);
	}
	
	public function add_worksheet(){
		$input = $this->input->post();
		$insert = array(
		'year_id' => str_replace('20','',date('y')),
		'month_id' => str_replace('0','',date('m')),
		'user_id' => $input['user_id'],
		'date_id' => ltrim(date('d'),'0'),
		'desc' => $input['desc'],
		'time_spent' => $input['hour'].':'.$input['min'].':'.$input['sec'],		
		);		
		$this->todo_model->add_worksheet($insert);
		redirect('user/to_do/view/'.$insert['user_id'].'/'.$insert['month_id'].'/'.$insert['year_id']);
	}
	/*public function get_details($id){
		return $this->todo_model->get_details($id);
	}*/
	
	/**
	* Founder part of todo
	*/
	public function view_all($date_id,$month_id,$year_id){
		$meta['page_title'] = 'All To Do';
		$get = array(
		'date_id' => $date_id,
		'month_id'=> $month_id,
		'year_id' => $year_id,		
		);		
		$this->data['users'] = $this->ion_auth->users()->result();
		$this->data['date_id'] = $date_id;
		$this->data['month_id'] = $month_id;
		$this->data['year_id'] = $year_id;
		$this->load->view('common/header',$meta);
		$this->load->view('founder/all-todo',$this->data);
		$this->load->view('common/footer');
	}
	public function add_fav(){		
		$this->todo_model->add_fav($_REQUEST['id']);
	}
	public function rem_fav($monthid,$yearid){		
		$this->todo_model->rem_fav($_REQUEST['id'],$_REQUEST['dateId'],$monthid,$yearid);
	}


}

?>